<template>
  <div class="box-table">
    <table cellspacing="0" class="table">
      <thead>
        <tr>
          <th width="50"><Checkbox v-model="selectAll" @on-change="onSelectAll"></Checkbox></th>
          <th width="20"></th>
          <th v-for="item in listData" :key="item.id" :width="item.width">
            <Dropdown @on-click="onBatchSet({setType: item.key, type: '0'})"> 
              <span>{{item.title}}</span> 
              <Icon v-if="item.key == '0' || item.key == '1' || item.key == '2' || item.key == '3' || item.key == '4'" class="cursor-title" type="ios-cog" />
              <DropdownMenu   slot="list"  v-if="item.key == '0' || item.key == '1' || item.key == '2' || item.key == '3' || item.key == '4'" class="cursor-title" type="ios-cog">
                <DropdownItem  >批量设置{{item.title}}</DropdownItem>
              </DropdownMenu>
            </Dropdown>
          </th>
        </tr>
      </thead>
      <tbody v-for="item in $store.state.bargain.bargainData" :key="item.id">
        <tr>
          <td><Checkbox v-model="item.checked"  @on-change="onJoinSaleCheck(item)"></Checkbox></td>
          <td class="cursor-title">
            <Icon type="ios-arrow-down" size="20" v-if="item.showChildren" @click="onShowChild(item)"></Icon>
            <Icon type="ios-arrow-forward" size="20" v-if="item.productSkuList && item.productSkuList.length && !item.showChildren" @click="onShowChild(item)"></Icon>
          </td>
          <td>{{item.code}}</td>
          <td>-</td>
          <td class="cursor-title" :title="item.title"><div class=" flex-start">
            <!-- <img :src="$store.state.moreSale.imgSrc+item.productPrimaryImg + '_90w'" alt="" > -->
            <span class="e2">{{item.title}}</span></div></td>
          <!-- 积分增加 -->
          <td>
            <Input @on-keyup="onTestValue(item, 'giftPoint')"  :min="0" style="width:60px;" maxlength="4" :max="999" @on-blur="onSetgiftPoint(item)" v-model="item.giftPoint"/>
          </td>
          <!-- 返利增加 -->
          <td>
            <Input   class="w40" style="width:60px;" maxlength="5" @on-blur="onSetGiftRebate(item)" v-model="item.giftRebate"/>
          </td>
          <td>
            <Select v-model="item.giftCouponId" :transfer="true" filterable  filter-by-label style="width:80px" @on-change="onSetgiftCouponId(item)">
                <Option v-for="item in bargainCouponsData" :value="item.value" :key="item.value" :label="item.label">{{ item.label }}</Option>
            </Select>
          </td>
          <!-- 库存 -->
          <td>
            <Input class="w40" @on-keyup="onTestValue(item, 'sellOutQuantity')" style="width:60px;" maxlength="6" @on-blur="onSetsellOutQuantity(item)" v-model="item.sellOutQuantity"/>
          </td>
          <!-- 阈值 -->
          <!-- <td>
            <Input class="w40" style="width:60px;" maxlength="4" @on-blur="onSetWarningQuantity(item)" v-model="item.warningQuantity"/>
          </td> -->
          <td>
            <Input maxlength="5"  class="w40"  style="width:60px"  @on-blur="onSetSale(item)" v-model="item.sale"/>
          </td>
        </tr>
        <template v-if="item.productSkuList && item.productSkuList.length">
          <tr  v-for="child in item.productSkuList" :key="child.id"  v-show="item.showChildren">
            <td></td>
            <td></td>
            <td><Checkbox v-model="child.checked" @on-change="onJoinSaleCheck(child,item)"></Checkbox></td>
            <td>{{child.code}}</td>
            <td class="cursor-title" :title="item.title"><div class="flex-start">
              <!-- <img :src="$store.state.moreSale.imgSrc+item.productPrimaryImg + '_90w'" alt=""> -->
              <span class="e2">{{item.title}}</span></div></td>
            <!-- 积分增加 -->
            <td>
              <Input @on-keyup="onTestValue(child, 'giftPoint')"  :min="0" style="width:60px;" maxlength="4" :max="999" @on-blur="onSetgiftPoint(child, item)" v-model="child.giftPoint"/>
            </td>
            <!-- 返利增加 -->
            <td>
              <Input class="w40" style="width:60px;"  maxlength="5" @on-blur="onSetGiftRebate(child, item)" v-model="child.giftRebate"/>
            </td>
            <td>
              <Select v-model="child.giftCouponId" filterable filter-by-label style="width:80px" :transfer="true">
                <Option v-for="item in bargainCouponsData" :value="item.value" :key="item.value" :label="item.label">{{ item.label }}</Option>
              </Select>
            </td>
            <!-- 库存 -->
            <td>
              <Input class="w40" @on-keyup="onTestValue(child, 'sellOutQuantity')" style="width:60px;" maxlength="6" @on-blur="onSetsellOutQuantity(child, item)" v-model="child.sellOutQuantity"/>
            </td>
            <!-- 阈值 -->
            <!-- <td>
              <Input class="w40" style="width:60px;" maxlength="4" @on-blur="onSetWarningQuantity(child, item)" v-model="child.warningQuantity"/>
            </td> -->
            <td>
              <Input maxlength="5"  class="w60"  style="width:60px"  @on-blur="onSetSale(child, item)" v-model="child.sale"/>
            </td>
          </tr>
        </template>
      </tbody>
    </table>
    <SetCoupon ref="editCoupon" ></SetCoupon>
    <BatchSetIntegral ref="editIntegral"></BatchSetIntegral>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import {getMinValue, getMaxValue, getSum} from "@/views/sales/common/TableSaleMethods"
import SetCoupon from '../edit/BatchSetCouponItem'
import BatchSetIntegral from '../edit/BatchSetIntegralItem'
export default {
  components: {
    SetCoupon,
    BatchSetIntegral
  },
  props: {
    listData: '',
    list:''
  },
  data() {
    return {
      status: 'pass',
      selectAll: false
    }
  },
  filters: {
    status (val) {
      if (val) {
        return '上架'
      }else {
        return '下架'
      }
    }
  },
  methods: {
    getSum(param, object) {
      return getSum(param, object)
    },
    getMinMaxValue(param, object) {
      if(getMinValue(param, object)==getMaxValue(param, object)) {
        return `[${getMinValue(param, object)}]`
      }else {
        return  `[${getMinValue(param, object)}-${getMaxValue(param, object)}]`;
      }
    },
    onSelectAll() {
      // 全选 、全不选
      this.$emit('onChangeSelectAll',this.selectAll);
    },
    onJoinSale (child, item) {
        this.$emit('onJoinSale',child, item);
    },
    onShowChild(item) {
      this.$emit('onShowChild', item)
    },
    onJoinSaleCheck (child,item) {
      this.$emit('onJoinSaleCheck', child, item, this.selectAll)
    },
    onSetgiftPoint(item, child) {
      this.onTestValue(item, 'giftPoint')
      this.$emit('onSetgiftPoint', item, child)
    },
    onSetGiftRebate(item, child) {
      this.onNegative(item, 'giftRebate')
      this.$emit('onSetGiftRebate', item, child)
    },
    onSetsellOutQuantity(item, child) {
      this.$emit('onSetsellOutQuantity', item, child)
    },
    onSetWarningQuantity(item, child) {
      this.$emit('onSetWarningQuantity', item, child)
    },
    onSetgiftCouponId(item) {
      this.$emit('onSetgiftCouponId', item)
    },
    onSetSale(item, child) {
      this.onNegative(item, 'sale')
      this.$emit('onSetSale', item, child)
    },
    onBatchSet(param) {
      let type = ''
      switch(param.setType) {
        case '0':
          type = 'integral';
        break;
        case '1':
          type = 'rebate'
        break;
        case '2':
          type = ''
        break;
        case '3':
          type = 'sellOutQuantity'
        break;
        case '4':
          type = 'sale'
        break;
      }
      if(type) {
        this.$refs.editIntegral.reset({setType: type, type: param.type})
      } else {
        this.$refs.editCoupon.reset({type: '0'})
      }
    },
    onTestValue(row, type) {
      if((!row[type] || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(row[type])) && row.productSkuList) {
        return false;
      }
      let value = JSON.parse(JSON.stringify(row))
      if(!(/^[1-9]\d*$/.test(row[type]))) {
        for (let i=0;  i<row[type].length ;i++) {
          if(!(/^[1-9]\d*$/.test(row[type][i]))) {
            row[type] = row[type].substring(0, i) + row[type].substring(i+1, row[type].length)
          }
        }
      }
    },
    onNegative(row, type) {
      if((!row[type] || !/^([\+ \-]?([1-9]\d*)|(0))([.]\d{0,2})?$/.test(row[type])) && row.productSkuList) {
        return false;
      }
      let value = JSON.parse(JSON.stringify(row))
      if(!(/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)|(0))$/.test(row[type]))) {
        for (let i=0;  i<row[type].length ;i++) {
          if(!(/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)|(0))$/.test(row[type][i]))) {
            row[type] = row[type].substring(0, i) + row[type].substring(i+1, row[type].length)
          }
        }
      }
    }
  },
  computed: {
    ...mapGetters([
      'bargainCouponsData'
    ])
  }
}
</script>

